package com.example.idachuappone.person;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.idachuappone.LoadingActivity;
import com.example.idachuappone.R;
import com.example.idachuappone.index.action.IndexTwoAction;
import com.example.idachuappone.index.dto.WelcomeDto;
import com.example.idachuappone.index.welcomentity.PersonExpands;
import com.example.idachuappone.person.entity.MemberTypee;
import com.example.idachuappone.person.item.CollectActivity;
import com.example.idachuappone.person.item.IdeaActivity;
import com.example.idachuappone.person.item.LoginActivity;
import com.example.idachuappone.person.item.PersonSettingActivity;
import com.example.idachuappone.person.member.MemberDetailActivity;
import com.example.idachuappone.person.member.MemberTypeActivity;
import com.example.idachuappone.person.member.MemberYuEActivity;
import com.example.idachuappone.person.method.UserInfoMethod;
import com.example.idachuappone.person.method.UserInfoMethod.CallBack;
import com.example.idachuappone.person.method.UserInfoMethod.CallBackString;
import com.example.idachuappone.person.youhui.MyYouHuiActivity;
import com.example.idachuappone.promptly.entity.UserInfo;
import com.example.idachuappone.utils.AppShareData;
import com.example.idachuappone.utils.BitmapUtilHelper;
import com.example.idachuappone.utils.Constant;
import com.example.idachuappone.utils.MainToast;
import com.example.idachuappone.utils.NetUtil;
import com.example.idachuappone.utils.PrefUtil;
import com.lidroid.xutils.BitmapUtils;
import com.lidroid.xutils.bitmap.BitmapGlobalConfig.BitmapCacheManagementTask;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;

public class PersonFragTwo extends Fragment implements OnClickListener {
	private int LoginRequest;
	private int MemberYuERequest;
	private Button btn_login;
	private BitmapUtils bu;
	private ImageView img_close_banner;
	private ImageView img_member_deg;
	private ImageView img_member_person_no;
	private List<MemberTypee> listmeMemberTypees;
	private LinearLayout ll_hezuo;
	private LinearLayout ll_my_member;
	private LinearLayout ll_no_member;
	private MemberTypee memberTypee;
	private RelativeLayout nowRL;
	private RelativeLayout rl_collect;
	private RelativeLayout rl_idea_feedback;
	private RelativeLayout rl_setting;
	private RelativeLayout rl_youhui;
	private TextView tv_member_yu_e;
	private TextView tv_phone;
	private TextView tv_quan_dian;
	private UserInfo userInfo;

	class AnonymousClass_3 implements OnClickListener {
		private final/* synthetic */PersonExpands val$personExpands;

		AnonymousClass_3(PersonExpands personExpands) {
			this.val$personExpands = personExpands;
		}

		@Override
		public void onClick(View v) {
			new IndexTwoAction().activityGoExpands(PersonFragTwo.this.getActivity(), this.val$personExpands);
		}
	}

	class AnonymousClass_1 extends RequestCallBack<String> {
		private final/* synthetic */boolean val$ismember;
		private final/* synthetic */int val$type;

		AnonymousClass_1(boolean z, int i) {
			this.val$ismember = z;
			this.val$type = i;
		}

		@Override
		public void onSuccess(ResponseInfo<String> responseInfo) {
			try {
				JSONObject jsonObject = new JSONObject(NetUtil.unescapeUnicode(responseInfo.result));
				if (jsonObject.getInt("code") == 10000) {
					int i;
					PersonFragTwo.this.listmeMemberTypees = new ArrayList();
					JSONArray jsonArray = jsonObject.getJSONArray("data");
					for (i = 0; i < jsonArray.length(); i++) {
						JSONObject object = jsonArray.getJSONObject(i);
						MemberTypee memberTypee = new MemberTypee();
						memberTypee.parseJson(object);
						PersonFragTwo.this.listmeMemberTypees.add(memberTypee);
					}
					if (PersonFragTwo.this.listmeMemberTypees != null
							&& PersonFragTwo.this.listmeMemberTypees.size() > 0) {
						PersonFragTwo.this.bu.display(PersonFragTwo.this.img_member_person_no,
								PersonFragTwo.this.listmeMemberTypees.get(0).getBanner());
					}
					if (this.val$ismember) {
						for (i = 0; i < PersonFragTwo.this.listmeMemberTypees.size(); i++) {
							if (this.val$type == Integer.parseInt(PersonFragTwo.this.listmeMemberTypees.get(i)
									.getDegree())) {
								PersonFragTwo.this.memberTypee = PersonFragTwo.this.listmeMemberTypees.get(i);
							}
						}
						if (this.val$type == 1) {
							PersonFragTwo.this.img_member_deg.setVisibility(0);
							PersonFragTwo.this.img_member_deg.setImageResource(R.drawable.per_ex_vip);
							return;
						} else if (this.val$type == 2) {
							PersonFragTwo.this.img_member_deg.setVisibility(0);
							PersonFragTwo.this.img_member_deg.setImageResource(R.drawable.general);
							return;
						} else if (this.val$type == 3) {
							PersonFragTwo.this.img_member_deg.setVisibility(0);
							PersonFragTwo.this.img_member_deg.setImageResource(R.drawable.per_sliver_vip);
							return;
						} else if (this.val$type == 4) {
							PersonFragTwo.this.img_member_deg.setVisibility(0);
							PersonFragTwo.this.img_member_deg.setImageResource(R.drawable.per_gold_vip);
							return;
						} else {
							PersonFragTwo.this.img_member_deg.setVisibility(0);
							PersonFragTwo.this.img_member_deg.setImageResource(R.drawable.open);
							return;
						}
					}
					PersonFragTwo.this.img_member_deg
							.setVisibility(BitmapCacheManagementTask.MESSAGE_CLEAR_MEMORY_BY_KEY);
				}
			} catch (JSONException e) {
				e.printStackTrace();
			}
		}

		@Override
		public void onFailure(HttpException arg0, String arg1) {
		}
	}

	public PersonFragTwo() {
		this.LoginRequest = 15;
		this.MemberYuERequest = 255;
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.bu = BitmapUtilHelper.createBitmapUtil(getActivity());
		initMemberData(false, -1);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.person_two, null);
		initView(view);
		return view;
	}

	private void initView(View view) {
		this.tv_quan_dian = (TextView) view.findViewById(R.id.tv_quan_dian);
		this.btn_login = (Button) view.findViewById(R.id.btn_login);
		this.btn_login.setOnClickListener(this);
		this.tv_phone = (TextView) view.findViewById(R.id.tv_phone);
		this.rl_collect = (RelativeLayout) view.findViewById(R.id.rl_collect);
		this.rl_collect.setOnClickListener(this);
		this.rl_youhui = (RelativeLayout) view.findViewById(R.id.rl_youhui);
		this.rl_youhui.setOnClickListener(this);
		this.rl_idea_feedback = (RelativeLayout) view.findViewById(R.id.rl_idea_feedback);
		this.rl_idea_feedback.setOnClickListener(this);
		this.rl_setting = (RelativeLayout) view.findViewById(R.id.rl_setting);
		this.rl_setting.setOnClickListener(this);
		this.img_close_banner = (ImageView) view.findViewById(R.id.img_close_banner);
		this.img_close_banner.setOnClickListener(this);
		this.ll_no_member = (LinearLayout) view.findViewById(R.id.ll_no_member);
		this.ll_no_member.setOnClickListener(this);
		this.img_member_person_no = (ImageView) view.findViewById(R.id.img_member_person_no);
		this.ll_my_member = (LinearLayout) view.findViewById(R.id.ll_my_member);
		this.ll_my_member.setOnClickListener(this);
		this.tv_member_yu_e = (TextView) view.findViewById(R.id.tv_member_yu_e);
		this.ll_hezuo = (LinearLayout) view.findViewById(R.id.ll_hezuo);
		this.img_member_deg = (ImageView) view.findViewById(R.id.img_member_deg);
		this.img_member_deg.setOnClickListener(this);
		initLlHezuo();
	}

	private void initMemberData(boolean ismember, int type) {
		NetUtil.get(getActivity(), Constant.MEMBER_LIST, 100, new AnonymousClass_1(ismember, type));
	}

	private void initLlHezuo() {
		if (LoadingActivity.welcome.getExpands() == null || LoadingActivity.welcome.getExpands().size() <= 0) {
			NetUtil.get(getActivity(), Constant.PACKAGES, 100, new RequestCallBack<String>() {
				@Override
				public void onFailure(HttpException arg0, String arg1) {
				}

				@Override
				public void onSuccess(ResponseInfo<String> responseInfo) {
					String json = NetUtil.unescapeUnicode(responseInfo.result);
					try {
						LoadingActivity.welcome = new WelcomeDto().getWelcome(PersonFragTwo.this.getActivity(), json);
						PersonFragTwo.this.setDataLlHeZuo();
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
			});
		} else {
			setDataLlHeZuo();
		}
	}

	public void setDataLlHeZuo() {
		for (int i = 0; i < LoadingActivity.welcome.getExpands().size(); i++) {
			View view = LinearLayout.inflate(getActivity(), R.layout.simple_person_hezuo_item, null);
			ImageView img = (ImageView) view.findViewById(R.id.imghezuo);
			PersonExpands personExpands = LoadingActivity.welcome.getExpands().get(i);
			this.bu.display(img, personExpands.getImg());
			img.setOnClickListener(new AnonymousClass_3(personExpands));
			this.ll_hezuo.addView(view);
		}
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.ll_no_member /* 2131165387 */:
			startActivity(new Intent(getActivity(), MemberTypeActivity.class));
		case R.id.img_member_deg /* 2131165490 */:
			if (this.memberTypee != null) {
				Intent intent1 = new Intent(getActivity(), MemberDetailActivity.class);
				intent1.putExtra("memberTypee", this.memberTypee);
				startActivity(intent1);
				return;
			}
			startActivity(new Intent(getActivity(), MemberTypeActivity.class));
		case R.id.btn_login /* 2131165491 */:
			Intent intentLogin = new Intent(getActivity(), LoginActivity.class);
			intentLogin.putExtra("islogin", true);
			startActivity(intentLogin);
		case R.id.img_close_banner /* 2131165497 */:
			PrefUtil.getInstance(getActivity()).setPersonImgBanner(
					Calendar.getInstance().get(BitmapCacheManagementTask.MESSAGE_CLEAR_MEMORY));
			this.ll_no_member.setVisibility(BitmapCacheManagementTask.MESSAGE_CLEAR_MEMORY_BY_KEY);
		case R.id.ll_my_member /* 2131165498 */:
			if (this.tv_member_yu_e.getText().toString() != null
					&& this.tv_member_yu_e.getText().toString().trim().length() > 0) {
				Intent intentmember = new Intent(getActivity(), MemberYuEActivity.class);
				intentmember.putExtra("memberType", this.userInfo.getDegree());
				startActivityForResult(intentmember, 888);
			}
		case R.id.rl_collect /* 2131165502 */:
			if (PrefUtil.getInstance(getActivity()).getUID() == null) {
				this.nowRL = this.rl_collect;
				showLogin();
				return;
			}
			startActivity(new Intent(getActivity(), CollectActivity.class));
		case R.id.rl_youhui /* 2131165504 */:
			if (PrefUtil.getInstance(getActivity()).getUID() == null) {
				this.nowRL = this.rl_youhui;
				showLogin();
				return;
			}
			startActivity(new Intent(getActivity(), MyYouHuiActivity.class));
			this.tv_quan_dian.setVisibility(BitmapCacheManagementTask.MESSAGE_CLEAR_MEMORY_BY_KEY);
		case R.id.rl_idea_feedback /* 2131165508 */:
			startActivity(new Intent(getActivity(), IdeaActivity.class));
		case R.id.rl_setting /* 2131165510 */:
			startActivity(new Intent(getActivity(), PersonSettingActivity.class));
		default:
		}
	}

	@Override
	public void onResume() {
		super.onResume();
		if (PrefUtil.getInstance(getActivity()).getUID() == null
				|| PrefUtil.getInstance(getActivity()).getUID().length() <= 0) {
			this.tv_quan_dian.setVisibility(BitmapCacheManagementTask.MESSAGE_CLEAR_MEMORY_BY_KEY);
			this.memberTypee = null;
			this.img_member_deg.setImageResource(R.drawable.open);
			this.img_member_deg.setVisibility(BitmapCacheManagementTask.MESSAGE_CLEAR_MEMORY_BY_KEY);
			this.ll_my_member.setVisibility(BitmapCacheManagementTask.MESSAGE_CLEAR_MEMORY_BY_KEY);
			if (Calendar.getInstance().get(BitmapCacheManagementTask.MESSAGE_CLEAR_MEMORY) == PrefUtil.getInstance(
					getActivity()).getPersonImgBanner()) {
				this.ll_no_member.setVisibility(BitmapCacheManagementTask.MESSAGE_CLEAR_MEMORY_BY_KEY);
			} else {
				this.ll_no_member.setVisibility(0);
				PrefUtil.getInstance(getActivity()).setPersonImgBanner(0);
			}
			this.btn_login.setVisibility(0);
			this.tv_phone.setVisibility(BitmapCacheManagementTask.MESSAGE_CLEAR_MEMORY_BY_KEY);
			return;
		}
		this.btn_login.setVisibility(BitmapCacheManagementTask.MESSAGE_CLEAR_MEMORY_BY_KEY);
		this.tv_phone.setVisibility(0);
		this.tv_phone.setText(PrefUtil.getInstance(getActivity()).getLoginPhone());
		if (AppShareData.isPersonFrag) {
			UserInfoMethod.getUserInfo(getActivity(), new CallBack() {
				@Override
				public void onSuccess(UserInfo userInfo) {
					PersonFragTwo.this.userInfo = userInfo;
					if (userInfo.getAddresses() != null && userInfo.getAddresses().size() > 0) {
						PrefUtil prefUtil = PrefUtil.getInstance(PersonFragTwo.this.getActivity());
						prefUtil.setFimallyLocateCoor(new StringBuilder(String.valueOf(userInfo.getAddresses().get(0)
								.getLng())).append(",").append(userInfo.getAddresses().get(0).getLat()).toString());
						prefUtil.setArea(userInfo.getAddresses().get(0).getArea());
						prefUtil.setXiaoQu(userInfo.getAddresses().get(0).getCommunity());
						prefUtil.setDoor(userInfo.getAddresses().get(0).getDoor_number());
						prefUtil.setName(userInfo.getAddresses().get(0).getName());
						prefUtil.setUserAddressId(userInfo.getAddresses().get(0).getId());
						prefUtil.setPhone(userInfo.getAddresses().get(0).getPhone());
						AppShareData.isPersonFrag = false;
					}
					if (userInfo.getDegree() == 0) {
						PersonFragTwo.this.memberTypee = null;
						PersonFragTwo.this.img_member_deg.setVisibility(0);
						PersonFragTwo.this.img_member_deg.setImageResource(R.drawable.open);
						PersonFragTwo.this.ll_my_member
								.setVisibility(BitmapCacheManagementTask.MESSAGE_CLEAR_MEMORY_BY_KEY);
						if (Calendar.getInstance().get(BitmapCacheManagementTask.MESSAGE_CLEAR_MEMORY) == PrefUtil
								.getInstance(PersonFragTwo.this.getActivity()).getPersonImgBanner()) {
							PersonFragTwo.this.ll_no_member
									.setVisibility(BitmapCacheManagementTask.MESSAGE_CLEAR_MEMORY_BY_KEY);
							return;
						}
						PersonFragTwo.this.ll_no_member.setVisibility(0);
						PrefUtil.getInstance(PersonFragTwo.this.getActivity()).setPersonImgBanner(0);
						return;
					}
					PersonFragTwo.this.ll_my_member.setVisibility(0);
					PersonFragTwo.this.ll_no_member
							.setVisibility(BitmapCacheManagementTask.MESSAGE_CLEAR_MEMORY_BY_KEY);
					PersonFragTwo.this.initMemberData(true, userInfo.getDegree());
					PersonFragTwo.this.setYuE();
				}

				@Override
				public void onFail() {
				}
			});
			isQuan();
		}
	}

	private void isQuan() {
		NetUtil.get(getActivity(), Constant.YOUHUINO, 100, new RequestCallBack<String>() {
			@Override
			public void onSuccess(ResponseInfo<String> responseInfo) {
				try {
					JSONObject jsonObject = new JSONObject(NetUtil.unescapeUnicode(responseInfo.result));
					if (jsonObject.getInt("code") == 10000) {
						JSONArray jsonArray = jsonObject.getJSONObject("data").getJSONArray("results");
						if (jsonArray.length() > 0) {
							if (Integer.parseInt(jsonArray.getJSONObject(0).optString("id")) > Integer
									.parseInt(PrefUtil.getInstance(PersonFragTwo.this.getActivity())
											.getYouhuiPersonDianId())) {
								PersonFragTwo.this.tv_quan_dian.setVisibility(0);
								return;
							} else {
								PersonFragTwo.this.tv_quan_dian
										.setVisibility(BitmapCacheManagementTask.MESSAGE_CLEAR_MEMORY_BY_KEY);
								return;
							}
						}
						PersonFragTwo.this.tv_quan_dian
								.setVisibility(BitmapCacheManagementTask.MESSAGE_CLEAR_MEMORY_BY_KEY);
					}
				} catch (Exception e) {
					e.printStackTrace();
					PersonFragTwo.this.tv_quan_dian
							.setVisibility(BitmapCacheManagementTask.MESSAGE_CLEAR_MEMORY_BY_KEY);
				}
			}

			@Override
			public void onFailure(HttpException arg0, String arg1) {
				PersonFragTwo.this.tv_quan_dian.setVisibility(BitmapCacheManagementTask.MESSAGE_CLEAR_MEMORY_BY_KEY);
			}
		});
	}

	private void setYuE() {
		UserInfoMethod.getUserYuE(getActivity(), new CallBackString() {
			@Override
			public void onSuccess(String result) {
				try {
					PersonFragTwo.this.tv_member_yu_e.setText(new JSONObject(result).optString("data"));
				} catch (JSONException e) {
					e.printStackTrace();
				}
			}

			@Override
			public void onFail() {
			}
		});
	}

	public void showLogin() {
		MainToast.show(getActivity(), "\u8bf7\u5148\u767b\u5f55", 0);
		startActivityForResult(new Intent(getActivity(), LoginActivity.class), this.LoginRequest);
	}

	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if (requestCode == this.LoginRequest) {
			getActivity();
			if (resultCode == -1 && this.nowRL != null) {
				this.nowRL.performClick();
			}
		}
		if (requestCode == this.MemberYuERequest) {
			getActivity();
			if (resultCode == -1 && data.getStringExtra("yue") != null && data.getStringExtra("yue").length() > 0) {
				this.tv_member_yu_e.setText(data.getStringExtra("yue"));
			}
		}
	}
}